package com.app.mingluxing.babytree;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.adapter.LocationAdapter;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import java.util.ArrayList;
import java.util.List;

/**
 * 位置页面
 * Created by Aaron on 15/7/30.
 */
public class LocationActivity extends BaseActivity {

    private ListView plv_search;
    private EditText et_search;
    private List<PoiInfo> allPoi;
    private static final int CHOOSE_LOCATION = 0x10002;
    private PoiSearch poiSearch;
    private LocationAdapter locationAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_location);

        initWidget();

        initPoiSearch();

//        getNearbyLocation("万达");

    }

    private void initWidget() {

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        Button tv_search = (Button) findViewById(R.id.tv_search);
        et_search = (EditText) findViewById(R.id.et_search);
        plv_search = (ListView) findViewById(R.id.plv_search);
//        plv_search = (PullToRefreshListView) findViewById(R.id.plv_search);

//        plv_search.setMode(PullToRefreshBase.Mode.BOTH);

        et_search.clearFocus();

        allPoi = new ArrayList<PoiInfo>();
        locationAdapter = new LocationAdapter(this, allPoi);
        plv_search.setAdapter(locationAdapter);

        plv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent data = new Intent();
                data.putExtra("location", allPoi.get(i).name);
                setResult(CHOOSE_LOCATION, data);
                finish();
            }
        });

        tv_title.setText(getResources().getString(R.string.location));

        iv_left.setOnClickListener(this);
        tv_search.setOnClickListener(this);

    }

    /**
     * 初始化poi检索
     */
    private void initPoiSearch() {

        poiSearch = PoiSearch.newInstance();

        OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {

            @Override
            public void onGetPoiResult(PoiResult poiResult) {

                if (poiResult != null) {

                    List<PoiInfo> allPoi = poiResult.getAllPoi();

                    if (allPoi!=null && allPoi.size() > 0) {

                        LocationActivity.this.allPoi.clear();
                        LocationActivity.this.allPoi.addAll(poiResult.getAllPoi());

                        locationAdapter.notifyDataSetChanged();

                    }else {
                        showToast("未查询到位置信息");
                    }

//                    List<PoiInfo> allPoi = poiResult.getAllPoi();
//
//                    if (allPoi != null && allPoi.size() > 0) {
//                        for (int i = 0; i < allPoi.size(); i++) {
//
//                            LogUtil.e("tag", "allPoi  ====  address:" + allPoi.get(i).address);
//
////                            LogUtil.e("tag", "allPoi  ====  name:" + allPoi.get(i).name);
//
//                        }
//                    }

                }else {
                    showToast("未查询到位置信息");
                }

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }
        };

        poiSearch.setOnGetPoiSearchResultListener(poiListener);

    }


    //获得附件的位置
    private void getNearbyLocation(String keyword) {

        String string = sp.getString(Constant.CITYNAME, "北京");
//        LogUtil.e("tag", "before  city   :    " + string);
        String city = "";
        if (!string.equals("北京") && string.length() > 0) {

            city = string.substring(0, string.length() - 1);

        } else {
            city = string;
        }

//        LogUtil.e("tag", "city   :    " + city);

        poiSearch.searchInCity((new PoiCitySearchOption()).city(city).keyword(keyword).pageCapacity(50).pageNum(0));

    }


    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_search: //搜索

                if (CommonUtils.isNull(et_search.getText().toString().trim())) {
                    showToast("请输入搜索内容");
                    return;
                }

//                if (CommonUtils.isNull(et_search.getText().toString().trim())) {
//                    showToast("请输入搜索内容");
//                    return;
//                }

                getNearbyLocation(et_search.getText().toString().trim());

                break;

        }
    }

}
